package controller;

import java.io.File;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import pojo.Paper;
import service.Paperservice;
import service.Studentservice;
import untils.Page;
@Controller
public class Papercontroller {
	@Autowired
	private Paperservice paperservice;

	public Paperservice getPaperservice() {
		return paperservice;
	}

	public void setPaperservice(Paperservice paperservice) {
		this.paperservice = paperservice;
	}
	@Autowired
	private Studentservice studentservice;
	public Studentservice getStudentservice() {
		return studentservice;
	}

	public void setStudentservice(Studentservice studentservice) {
		this.studentservice = studentservice;
	}

	/**
	 *  查询论文列表
	 */
	@RequestMapping(value = "/paperfindall")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, 
			String name, String studentname, String paperclass, Model model) {
		// 条件查询所有论文
		Page<Paper> papers = paperservice.findpaperList(page, rows, name, studentname, paperclass);
		model.addAttribute("page",papers);
		// 添加参数
		model.addAttribute("name", name);
		model.addAttribute("studentname", studentname);
		model.addAttribute("paperclass", paperclass);
		return "paper";
	}
	
	//上传
	@RequestMapping("/paperupload")
	public String handleFormUpload(Paper paper,@RequestParam("upload") List<MultipartFile> upload,
			HttpServletRequest request) {
		String f=null;
		// 判断所上传文件是否存在
		if (!upload.isEmpty() && upload.size() > 0) {
			//循环输出上传的文件
			for (MultipartFile file : upload) {
				// 获取上传文件的原始名称
				String originalFilename = file.getOriginalFilename();
				f=file.getOriginalFilename();
				// 设置上传文件的保存地址目录
				//String dirPath = request.getServletContext().getRealPath("/upload/");
				String dirPath = "D:\\eclipse\\java\\Test1842\\upload\\";
				File filePath = new File(dirPath);
				// 如果保存文件的地址不存在，就先创建目录
				if (!filePath.exists()) {
					filePath.mkdirs();
				}
				// 使用UUID重新命名上传的文件名称(上传人_uuid_原始文件名称)
				/*
				 * String newFilename = name+ "_"+UUID.randomUUID() + "_"+originalFilename;
				 */
				try {
					// 使用MultipartFile接口的方法完成文件上传到指定位置
					file.transferTo(new File(dirPath+originalFilename));
				} catch (Exception e) {
					e.printStackTrace();
					
				}
			}
		}
		//设置论文信息
		paper.setPlace(f);
		paperservice.increase(paper);
		studentservice.increasepapernum(paper.getStudentID());
		return "redirect:paper";
	}
	
	
	
	//下载
	@RequestMapping("/download")
	public ResponseEntity<byte[]> fileDownload(int ID,HttpServletRequest request,HttpSession session) throws Exception{
		
			Paper p = paperservice.find(ID);
			String filename = p.getPlace();
			System.out.println(filename);
			// 指定要下载的文件所在路径
		    // String path = request.getServletContext().getRealPath("/upload/");
			String path = "D:\\eclipse\\java\\Test1842\\upload\\";
		    // 创建该文件对象
		    File file = new File(path+File.separator+filename);
		    // 对文件名编码，防止中文文件乱码
		    filename = this.getFilename(request, filename);
		    // 设置响应头
		    HttpHeaders headers = new HttpHeaders();
		    // 通知浏览器以下载的方式打开文件
		    headers.setContentDispositionFormData("attachment", filename);
		    // 定义以流的形式下载返回文件数据
		    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		    // 使用Sring MVC框架的ResponseEntity对象封装返回下载数据
		    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                    headers,HttpStatus.OK);
	}
	
	
	
	
	
	
	public String getFilename(HttpServletRequest request,String filename) throws Exception { 
		// IE不同版本User-Agent中出现的关键词
		String[] IEBrowserKeyWords = {"MSIE", "Trident", "Edge"};  
		// 获取请求头代理信息
		String userAgent = request.getHeader("User-Agent");  
		for (String keyWord : IEBrowserKeyWords) { 
			if (userAgent.contains(keyWord)) { 
				//IE内核浏览器，统一为UTF-8编码显示
				return URLEncoder.encode(filename, "UTF-8");
			}
		}  
		//火狐等其它浏览器统一为ISO-8859-1编码显示
		return new String(filename.getBytes("UTF-8"), "ISO-8859-1");  
	}
	
	
}
